package com.jhws.business.car.service;

import com.jhws.business.car.bo.ParkingLot;
import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;
import com.jhws.util.GetId;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("Duplicates")
@Service
public class ParkingLotService {
    @Resource
    private BaseDao BaseDao;

    /**
     * 保存停车场信息
     *
     * @param parkingLot 停车场信息
     */
    public void saveParkingLot(ParkingLot parkingLot) {
        if (StringUtils.isEmpty(parkingLot.getParkId())) {
            parkingLot.setParkId(GetId.randomID());
            parkingLot.setCreationTime(new Date());
        }
        BaseDao.saveOrUpdate(parkingLot);
    }

    /**
     * 保存月卡费率
     */
    public void saveMonthlyRate(String parkId, int monthlyRate) {
        ParkingLot parkingLot = queryParkingLotInfo(parkId);
        parkingLot.setMonthlyRate(monthlyRate);
        BaseDao.saveOrUpdate(parkingLot);
    }

    /**
     * 查询停车场列表
     *
     * @param param Map
     * @return 停车场列表
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> queryParkingLotList(Map param) {
        String commCode = MapUtils.getString(param, "commCode", "");
        boolean isNotPage = MapUtils.getBooleanValue(param, "isNotPage");
        String sql = "SELECT * " + " FROM car_parking_lot  WHERE 1=1 ";
        if (StringUtils.isNotEmpty(commCode)) {
            sql += " and comm_code = '" + commCode + "' ";
        } else {
            sql += " and comm_code is null or trim(comm_code)=''";
        }
        sql += " order by creation_time desc";
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list;
        if (isNotPage) {
            list = BaseDao.j().queryForList(sql);
        } else {
            Page p = BaseDao.queryPage(sql, Map.class);
            list = p.getList();
            result.put("size", p.getTotalNumber());
        }
        result.put("list", list);
        result.put("ret_code", 1);
        return result;
    }

    /**
     * 条件查询停车场列表
     *
     * @param where
     * @return
     */
    public List<ParkingLot> queryParkingLostByWhere(String where) {
        return BaseDao.findList(" from ParkingLot " + where);
    }

    /**
     * 删除停车场信息
     *
     * @param parkingLot 停车场ID
     */
    public void deleteParkingLot(ParkingLot parkingLot) {
        BaseDao.h().delete(parkingLot);
    }

    /**
     * 查询停车场信息
     *
     * @param id 停车场ID
     */
    public ParkingLot queryParkingLotInfo(String id) {
        return BaseDao.h().get(ParkingLot.class, id);
    }
}
